Within a typical computer system, a common task is the conversion of data between different number bases, for example between base-2 (binary) and base-10 (decimal) representations.
Although human users are familiar with decimal numbering, computers generally store numeric values (e.g., integer or floating-point values) in binary format. Such numeric values understood by the computer in their binary format must generally be converted to a corresponding decimal representation before they can be presented within a software application (for example, a business application) that is designed for access by a user.
However, an issue commonly arises with numeric values that include fractions. While integers can be exactly represented in any number base, fractions generally cannot. For example, the fraction 1/10 can be exactly represented in decimal (as 0.1), but cannot be exactly represented in binary, and instead must be converted to a binary approximation (i.e., 0.00011 . . . ). Similarly, although the fraction ⅓ can be exactly represented in base-3 (as 0.1), it cannot be exactly represented in decimal, and must likewise be converted to a decimal approximation (i.e., 0.33333 . . . ).
Computer systems typically address this issue by converting binary format numeric values to human-readable decimal approximations that are inexact representations of the original values, but are of sufficient precision for their intended use. For example, in a Java environment, floating-point data can be represented using the IEEE Standard for Floating-Point Arithmetic (IEEE 754) as binary double values, which are then converted to decimal—possibly inexactly—for display purposes. Software applications can then present the converted values to end-users, or store them as textual data, using their human-readable decimal representations.
The decimal representation ultimately provided by the computer system may depend on the target locale associated with a particular software application. For example, the United States of America and India each recognize different schemes to represent large numbers, such as inserting grouping characters (e.g., spaces or commas) at different locations within the number. Other examples of locale-specific differences include the various sets of characters used to represent digits. As such, business applications intended for use in different countries may be required to represent their numbers accordingly.
Generally, when a floating-point value is to be represented in a human-readable form, the system can convert the value's binary format to a decimal representation, which in some environments can be implemented within a software library as a decimal-to-ascii (“dtoa”) function. Using such a process, each decimal digit of the human-readable form is extracted from the binary format representation, in a one-by-one manner, using mathematical polynomial properties of the binary and decimal representations. Different mechanisms for converting between binary and decimal formats can be used, depending on the particular goals desired. Examples include (a) use of a fixed total number of digits chosen ahead of time; (b) use of a fixed number of digits to the right of the decimal point; (c) use of just enough digits to recover the original value (which is the default output mechanism in Java); or (d) use of as many digits as needed to output the exact numerical result (which can be up to several hundred places).
However, this process of conversion can be computationally expensive. Additionally, preparing the final decimal representation for use with a particular target locale may necessitate a further conversion, from an initial textual representation provided by a dtoa function, to the ultimate representation required by the target locale, requiring yet further computation.
The above are some examples of the types of environments in which embodiments of the invention can be used.